package com.data.common.api.vo;

import com.data.common.constant.CommonConstant;
import com.fasterxml.jackson.annotation.JsonIgnore;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;

/**
 *   自定义CRM 接口返回数据格式
 */
@SuppressWarnings("ALL")
@Data
@ApiModel(value="接口返回对象", description="接口返回对象")
public class DeviceResult<T> implements Serializable {

	private static final long serialVersionUID = 1L;


	/**
	 * 返回处理消息
	 */
	@ApiModelProperty(value = "返回处理消息" ,example = "success")
	private String msg = "";


	/**
	 * 返回数据对象 data
	 */
	@ApiModelProperty(value = "返回数据对象")
	private T data;

	/**
	 * 时间戳
	 */
	@ApiModelProperty(value = "时间戳")
	private String time = String.valueOf(System.currentTimeMillis());

	/**
	 * 返回处理消息
	 */
	@ApiModelProperty(value = "控制器时间戳(s),需与设备上报一至" )
	private String TimeStamp = "";

	public DeviceResult() {
	}

    /**
     * 兼容VUE3版token失效不跳转登录页面
     * @param code
     * @param msg
     */
	public DeviceResult(String TimeStamp, String msg) {
		this.TimeStamp = TimeStamp;
		this.msg= msg;
	}
/*
	public DeviceResult<T> success(String msg) {
		this.msg = msg;
		this.code = 0;
		return this;
	}*/

	//@Deprecated
	public static DeviceResult<Object> ok() {
		DeviceResult<Object> r = new DeviceResult<Object>();
		//r.setCode(0);
		return r;
	}

	//@Deprecated
	public static DeviceResult<Object> ok(String msg) {
		DeviceResult<Object> r = new DeviceResult<Object>();
		//r.setCode(0);
		r.setMsg(msg);
		return r;
	}

	//@Deprecated
	public static DeviceResult<Object> ok(Object data,String timeStamp) {
		DeviceResult<Object> r = new DeviceResult<Object>();
		r.setMsg("success");
		r.setTimeStamp(timeStamp);
		r.setData(data);
		return r;
	}

	public static<T> DeviceResult<T> OK() {
		DeviceResult<T> r = new DeviceResult<T>();
		//r.setCode(0);
		return r;
	}

	public static<T> DeviceResult<T> OK(String msg) {
		DeviceResult<T> r = new DeviceResult<T>();
		//r.setCode(0);
		r.setMsg(msg);
		//Result OK(String msg)方法会造成兼容性问题 issues/I4IP3D
		r.setData((T) msg);
		return r;
	}

	public static<T> DeviceResult<T> OK(T data) {
		DeviceResult<T> r = new DeviceResult<T>();
		//r.setCode(0);
		r.setData(data);
		return r;
	}

	public static<T> DeviceResult<T> OK(String msg, T data) {
		DeviceResult<T> r = new DeviceResult<T>();
		//r.setCode(0);
		r.setMsg(msg);
		r.setData(data);
		return r;
	}

	public static<T> DeviceResult<T> error(String msg, T data) {
		DeviceResult<T> r = new DeviceResult<T>();
		//r.setCode(CommonConstant.SC_INTERNAL_SERVER_ERROR_500);
		r.setMsg(msg);
		r.setData(data);
		return r;
	}

	public static DeviceResult<Object> errorMsg(String errorMsg) {
		return error(CommonConstant.SC_INTERNAL_SERVER_ERROR_500, "网络异常",errorMsg);
	}

	public static DeviceResult<Object> error(String msg) {
		return error(CommonConstant.SC_INTERNAL_SERVER_ERROR_500, msg);
	}

	public static DeviceResult<Object> error(int code, String msg) {
		DeviceResult<Object> r = new DeviceResult<Object>();
		//r.setCode(code);
		r.setMsg(msg);
		return r;
	}

	public static DeviceResult<Object> error(int code, String msg, String errorMsg) {
		DeviceResult<Object> r = new DeviceResult<Object>();
		r.setMsg(msg);
		//r.setErrorMsg(errorMsg);
		return r;
	}


	public DeviceResult<T> error500(String msg) {
		this.msg = msg;
		//this.code = CommonConstant.SC_INTERNAL_SERVER_ERROR_500;
		return this;
	}
	/**
	 * 无权限访问返回结果
	 */
	public static DeviceResult<Object> noauth(String msg) {
		return error(401, msg);
	}

	@JsonIgnore
	private String onlTable;

}
